package mysqldao

import (
	"ahutoj/web/dao"
	"context"
	"errors"

	"gorm.io/gorm"
)

func SelectContestByCID(ctx context.Context, CID int64) (*dao.Contest, error) {
	db := GetDB(ctx)
	ret := new(dao.Contest)
	err := db.Table(ret.TableName()).Where("CID=?", CID).Find(ret).Error
	return ret, err
}

func SelectBaseContestByCID(ctx context.Context, CID int64) (*dao.Contest, error){
	db := GetDB(ctx)
	ret := new(dao.Contest)
	err := db.Table(ret.TableName()).Where("CID=?", CID).Find(ret).Error
	return ret,err
}

func SelectContests(ctx context.Context, offset, limit int) ([]dao.Contest, error) {
	db := GetDB(ctx)
	tp := dao.Contest{}
	ret := make([]dao.Contest, 0)
	err := db.Table(tp.TableName()).Order("EndTime desc").Where("Type <> ?", -1).Offset(offset).Limit(limit).Find(&ret).Error //.Order("EndTime desc")貌似不需要？
	return ret, err
}
func SelectTrains(ctx context.Context, offset, limit int) ([]dao.Contest, error) {
	db := GetDB(ctx)
	tp := dao.Contest{}
	ret := make([]dao.Contest, 0)
	err := db.Table(tp.TableName()).Where("Type = ?", -1).Offset(offset).Limit(limit).Find(&ret).Error
	return ret, err
}
func DeleteContest(ctx context.Context, CIDs []int64) error {
	db := GetDB(ctx)
	var err error
	tp := dao.Contest{}
	for _, CID := range CIDs {
		err = db.Table(tp.TableName()).Where("CID=?", CID).Delete(&CID).Error
	}
	return err
}

func InserContest(ctx context.Context, contest dao.Contest) error {
	db := GetDB(ctx)
	err := db.Table(contest.TableName()).Create(&contest).Error
	return err
}

func SelectContestByUID(ctx context.Context, UID string) (int64, error) {
	db := GetDB(ctx)
	ret := dao.Contest{}
	err := db.Where("UID=?", UID).Last(&ret).Error
	return ret.CID, err
}

func UpdateContest(ctx context.Context, contest dao.Contest) error {
	db := GetDB(ctx)
	err := db.Table(contest.TableName()).Where("CID=?", contest.CID).Updates(&contest).Error
	return err
}

func SelectContestCount(ctx context.Context) (int64, error) {
	db := GetDB(ctx)
	contest := dao.Contest{}
	count := int64(0)
	err := db.Table(contest.TableName()).Where("Type <> ?", -1).Count(&count).Error
	return count, err
}

func SelectTrainCount(ctx context.Context) (int64, error) {
	db := GetDB(ctx)
	contest := dao.Contest{}
	count := int64(0)
	err := db.Table(contest.TableName()).Where("Type = ?", -1).Count(&count).Error
	return count, err
}
func SelectContestProblemByCID(ctx context.Context, CID int64) (problems []dao.ConPro, err error){
	db := GetDB(ctx)
	err = db.Table(dao.ConPro{}.TableName()).Where("CID=?", CID).Find(&problems).Error
	return problems, err
}


func SelectContestRecently(ctx context.Context, Recently int64) (contests []dao.Contest, err error) {
	db := GetDB(ctx)
	contest := dao.Contest{}
	err = db.Table(contest.TableName()).Where("EndTime>?", Recently).Find(&contests).Error
	return contests, err
}

func SelectMyStatus(ctx context.Context, UID string, PID string,CID int64) (string, error) {
	db := GetDB(ctx)
	ret := dao.Submit{
		CID: CID,
		UID: UID,
		PID: PID,
	}
	//先看有没有AC的，然后再随便选个错误
	if err := db.Table(ret.TableName()).Where(&ret).Where("ResultACM=?","AC").First(&ret).Error; err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			return "", err	
		} 
	} else{
		return "AC", nil
	}
	if err := db.Table(ret.TableName()).Where(&ret).Last(&ret).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return "", nil 
		}
		return "", err 
	} else {
		return string(ret.Result), nil
	}
}
// func NotFoundToNil(err error)(error){
// 	if err!=nil{
// 		if errors.Is(err, gorm.ErrRecordNotFound) {
// 			return nil
// 		}else{
// 			return err
// 		}
// 	}
// 	return nil
// } 
